/**
  * <br>JAVACC DEMO 1.0<br>
*/
package com.apache.portal.weixin.core.common;

import javax.servlet.http.HttpServletRequest;

import com.apache.portal.common.util.EnumUtil;
import com.apache.portal.weixin.core.manager.BeanFactory;
import com.apache.portal.weixin.core.manager.ResMsgManager;
import com.apache.portal.weixin.entity.recv.WxRecvEventMsg;
import com.apache.portal.weixin.entity.recv.WxRecvGeoMsg;
import com.apache.portal.weixin.entity.recv.WxRecvMsg;
import com.apache.portal.weixin.entity.recv.WxRecvPicMsg;
import com.apache.portal.weixin.entity.recv.WxRecvTextMsg;
import com.apache.portal.weixin.entity.recv.WxRecvVideoMsg;
import com.apache.portal.weixin.entity.recv.WxRecvVoiceMsg;
import com.apache.portal.weixin.entity.send.WxSendMsg;
import com.apache.portal.weixin.entity.send.WxSendTextMsg;
import com.apache.uct.common.ToolsUtil;

/**
  * description: 核心业务
  * @author Hou DaYu 创建时间：下午06:17:51
  */
public class CoreService {

	/**
	  * description: 消息处理核心方法
	  * @param request
	  * @return
	 */
	public WxSendMsg processRequest(HttpServletRequest request) throws Exception {

		LoadService.getInstance().init();//初始化配置文件

		WxRecvMsg msg = LoadService.getInstance().getTools().recv(request.getInputStream());
		WxSendMsg sendMsg = LoadService.getInstance().getTools().builderSendByRecv(msg);
		ResMsgManager resMsgManager = null;
		if (msg instanceof WxRecvTextMsg) {//1.接受到的文本消息，回复处理
			sendMsg = textMsgService(msg, sendMsg);
		} else if (msg instanceof WxRecvVoiceMsg) {//2.接受到的语音/音乐消息，回复处理
			if ("music".equalsIgnoreCase(msg.getMsgType())) {
				resMsgManager = BeanFactory.getInstance().getBeans("orther");
				sendMsg = resMsgManager.execute(EnumUtil.ResMsgEnum.music, msg);
			} else {
				resMsgManager = BeanFactory.getInstance().getBeans("orther");
				sendMsg = resMsgManager.execute(EnumUtil.ResMsgEnum.voice, msg);
			}
		} else if (msg instanceof WxRecvPicMsg) {//3.接受到的图片消息，回复处理
			resMsgManager = BeanFactory.getInstance().getBeans("pic");
			sendMsg = resMsgManager.execute(EnumUtil.ResMsgEnum.pic, msg);
		} else if (msg instanceof WxRecvEventMsg) {//4.接受到的事件消息
			sendMsg = eventMsgService(msg, sendMsg);
		} else if (msg instanceof WxRecvGeoMsg) {//5.接受到的地理位置信息
			//WxRecvGeoMsg recvMsg = (WxRecvGeoMsg) msg;
			//			BeanFactory.getInstance().setBeans(new BaiduMapManagerImpl(), "baidumap");
			//			resMsgManager = BeanFactory.getInstance().getBeans("baidumap");
			//			sendMsg = resMsgManager.execute(ResMsgManager.ResMsgEnum.location, msg);
		} else if (msg instanceof WxRecvVideoMsg) {//6.接受到的视频消息
			resMsgManager = BeanFactory.getInstance().getBeans("orther");
			sendMsg = resMsgManager.execute(EnumUtil.ResMsgEnum.video, msg);
		} else {//8.接受到的未能识别的消息
			resMsgManager = BeanFactory.getInstance().getBeans("orther");
			sendMsg = resMsgManager.execute(EnumUtil.ResMsgEnum.orther, msg);
		}
		return sendMsg;
	}

	/**
	  * description: 文本消息接处理核心方法
	  * @param msg
	  * @return
	 */
	private WxSendMsg textMsgService(WxRecvMsg msg, WxSendMsg sendMsg) {
		ResMsgManager resMsgManager = null;
		WxRecvTextMsg recvMsg = (WxRecvTextMsg) msg;

		resMsgManager = BeanFactory.getInstance().getBeans("text");
		sendMsg = resMsgManager.execute(EnumUtil.ResMsgEnum.getTextMsg(recvMsg.getContent()), msg);
		return sendMsg;
	}

	/**
	  * description: 事件接处理核心方法
	  * @param msg
	  * @return
	 */
	private WxSendMsg eventMsgService(WxRecvMsg msg, WxSendMsg sendMsg) {
		ResMsgManager resMsgManager = null;
		WxRecvEventMsg recvMsg = (WxRecvEventMsg) msg;
		String event = recvMsg.getEvent();
		if ("subscribe".equalsIgnoreCase(event)) {//用户关注微信事件
			resMsgManager = BeanFactory.getInstance().getBeans("text");
			sendMsg = resMsgManager.execute(EnumUtil.ResMsgEnum.event, msg);
			//sendMsg = new WxSendTextMsg(sendMsg, "谢谢您的关注，请多提意见，我们会努力做到更好。");
		} else if ("unsubscribe".equalsIgnoreCase(event)) {
			// 取消订阅
			sendMsg = new WxSendTextMsg(sendMsg, "谢谢您对我们长期以来的支持，希望您能继续关注我们。");
		} else if ("CLICK".equalsIgnoreCase(event)) {
			String keys = ToolsUtil.getInstance().getValueByKey("menu_key").toLowerCase();
			// 自定义菜单点击事件
			String eventKey = recvMsg.getEventKey();
			// 判断自定义菜单中的key回复消息
			if (keys.indexOf(eventKey.toLowerCase()) != -1) {
				resMsgManager = BeanFactory.getInstance().getBeans("text");
				sendMsg = resMsgManager.execute(EnumUtil.ResMsgEnum.event, msg);
			} else {
				resMsgManager = BeanFactory.getInstance().getBeans("orther");
				sendMsg = resMsgManager.execute(EnumUtil.ResMsgEnum.orther, msg);
			}
		} else if ("LOCATION".equalsIgnoreCase(event)) {//地理位置信息

		} else if ("VIEW".equalsIgnoreCase(event)) {//点击菜单跳转链接时的事件推送
			resMsgManager = BeanFactory.getInstance().getBeans("event");
			sendMsg = resMsgManager.execute(EnumUtil.ResMsgEnum.event, msg);
		} else {
			sendMsg = new WxSendTextMsg(sendMsg, "无法识别的事件消息");
		}
		return sendMsg;
	}

}
